using System.Activities.Statements;
using System.Collections.Generic;
using System.Linq;
using Headsight.BPMN2WF.BPMN.Model;
using Headsight.BPMN2WF.Transformation;
using Headsight.BPMN2WF.Transformation.Transformers.Additions;

namespace Headsight.BPMN2WF.WF.Simplification
{
    /// <summary>
    /// <see cref="IBPMN2WFTransformation"/> implementation trying
    /// to <i>simplify</i> created <i>WF artifacts</i> before delivery upper
    /// the transformation chain. An example is the merging to two <see cref="Flowchart"/>s
    /// whereas one <see cref="Flowchart"/> is a child of the other <see cref="Flowchart"/>.
    /// Obviously such constellations may be combined into one single <see cref="Flowchart"/>.
    /// </summary>
    public class BPMN2WFTransformationSimplifier : BPMN2WFTransformationDelegateBaseImpl
    {
        private static IList<IWFSimplifier> Simplifiers
        {
            get
            {
                return new List<IWFSimplifier>
                {
                    new WFSimplifierSequence(), 
                    new WFSimplifierIfThenSequence(), 
                    new WFSimplifierFlowchart()
                };
            }
        }

        private readonly IList<IWFSimplifier> _simplifiers;

        public BPMN2WFTransformationSimplifier(IBPMN2WFTransformation @delegate) : this(@delegate, Simplifiers)
        {
            // ^^
            // Works for now implementation...
            // Eventually this whole simplification stuff will / should be
            // implemented differently in future... But... at least this allows
            // me to test the individual simplifications strategies isolated
            // from each other...
        }

        public BPMN2WFTransformationSimplifier(IBPMN2WFTransformation @delegate, IList<IWFSimplifier> simplifiers) : base(@delegate)
        {
            _simplifiers = simplifiers;
        }

        public override BPMNTransformationResult Transform(IBPMN2WFTransformationContext ctx, BPMNFlowNode flowNode)
        {
            var result = Delegate.Transform(ctx, flowNode);

            foreach (var each in _simplifiers.Where(each => each.Probe(ctx, result)))
            {
                result = each.Simplify(ctx, result);
            }

            return result;
        }



    }
}